package com.xin.libandroidpaysocial.pay.weixin;

import android.app.Activity;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WPay {
    private final IWXAPI api;
    Activity context;

    public WPay(Activity context) {
        this.context = context;
        api = WXAPIFactory.createWXAPI(context, null);

    }

    public void doPay(WPayBean data) {
        if (!api.isWXAppInstalled()) {
            toast("您还未安装微信客户端");
            return;
        }
        PayReq req = new PayReq();
        req.appId = data.getAppid();
        req.partnerId = data.getPartnerid();
        req.prepayId = data.getPrepayid();
        req.nonceStr = data.getNoncestr();
        req.timeStamp = data.getTimestamp() + "";
        req.packageValue = data.getPackageX();
        req.sign = data.getSign();
        req.extData = "app data"; // optional
        api.registerApp(data.getAppid());
        api.sendReq(req);
    }

    private void toast(String str) {
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }
}
